package com.study.zzedu.fragment.coursetab;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.study.zzedu.widget.bean.TreeNode;
import com.study.zzedu.R;
import com.study.zzedu.adapter.TeachProgramAdapter;
import com.study.zzedu.bean.BranchNode;
import com.study.zzedu.bean.LeafNode;
import com.study.zzedu.bean.LeafNode2;
import com.study.zzedu.bean.RootNode;
import com.study.zzedu.entity.CourseDetailItem;
import com.study.zzedu.entity.TeachProgramItem;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.study.zzedu.utils.AppConstant.courseDetail;

/**
 * 作者：Russell on 2020/11/24 15:42
 */
public class TeachProgramFragment extends Fragment {

    TeachProgramAdapter teachProgramAdapter;
    RecyclerView mRecyclerView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_teaching_program, container, false);
        initView(rootView);
        return rootView;
    }
    protected void initView(View view) {

        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_list_data);
        LinearLayoutManager lm = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(lm);

        if(null != AppConstant.courseDetail){
            List<TeachProgramItem> sData = new ArrayList<>();

            add(sData,view);
            teachProgramAdapter = new TeachProgramAdapter(sData);
            //mRecyclerView.addItemDecoration(new SpacesItemDecoration(30));
            mRecyclerView.setAdapter(teachProgramAdapter);
        }
    }

    private void add(List<TeachProgramItem> sData,View view){
        try {
            JSONArray jArray = courseDetail;
            //第一层循环
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject job1 = jArray.getJSONObject(i);
                try{
                    if (job1.has("children")){
                        JSONArray jArray1 = job1.getJSONArray("children");
                        sData.add(new TeachProgramItem(TeachProgramItem.Type.FIRST,job1.getString("title")));
                        //第二层循环（二级菜单）
                        for (int y = 0; y < jArray1.length(); y++) {
                            JSONObject job2 = jArray1.getJSONObject(y);
                            //有“子”加载枝节点
                            if(job2.has("children")){
                                JSONArray jArray2 = job2.getJSONArray("children");
                                sData.add(new TeachProgramItem(TeachProgramItem.Type.SENCOD,job2.getString("title")));
                                //第三次循环（三级菜单）----最多支持三级
                                for (int p=0;p<jArray2.length();p++){
                                    JSONObject job3 = jArray2.getJSONObject(p);
                                    if(!TextUtils.isEmpty(job3.getString("title")) && !TextUtils.isEmpty(job3.getString("dataType"))){
                                        //String s = ("课件".equals(job3.getString("dataType")))?job3.getString("contentType"):job3.getString("dataType");
                                        sData.add(new TeachProgramItem(TeachProgramItem.Type.THIED,job2.getString("title")));
                                    }
                                }
                            }else {
                                if("目录".equals(job2.getString("dataType"))){
                                    //处理空目录情况
                                    sData.add(new TeachProgramItem(TeachProgramItem.Type.SENCOD,job2.getString("title")));
                                }else {
                                    //无“子”加载叶节点
                                    if(!TextUtils.isEmpty(job2.getString("title")) && !TextUtils.isEmpty(job2.getString("dataType"))){
                                        //String s = ("课件".equals(job2.getString("dataType")))?job2.getString("contentType"):job2.getString("dataType");
                                        sData.add(new TeachProgramItem(TeachProgramItem.Type.SENCOD,job2.getString("title")));
                                    }
                                }

                            }
                        }
                    }

                }catch (Exception e){
                    e.toString();
                }
            }
            view.findViewById(R.id.rl_default_teach_program).setVisibility(View.GONE);
        } catch (Exception e) {
            e.toString();
        }
    }
}
